<?php

namespace App\Http\Controllers\App;

use App\Models\Wares;
use App\Models\Queue;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WaresController extends Controller
{
    /**
     * 商品列表(GET) api/wares/list
     */
    public function list(Request $request) 
    {   
        // 用户归属商户id
        $shop_id = $request->input('shop_id');
        $queue   = $request->input('queue');
        if(!$shop_id || !$queue){
            return $this->error('参数缺省');
        }

        // 队列消费额度
        $queue_info = Queue::where('id',$queue)->select('min_amount','max_amount')->first();

        // 获取绑定店铺下的商品列表
        $queue_info['goods'] = Wares::where('shop_id',$shop_id)
                            ->where('status',1)
                            ->where('delete',1)
                            ->select('id','name','sell_price','logo','spec')
                            ->paginate(10);


        // 数据处理
        $goods = $this->dealData($queue_info);

        return self::json($goods,'成功');
    }

    /**
     * 商品详情(GET) api/wares/info
     */
    public function info(Request $request)
    {   
        // 商品id
        $id = $request->input('id');
        if(!$id){
            return $this->error('商品参数有误');
        }

        // 获取商品详情
        $info = Wares::where('id',$id)
                ->select('id','name','logo','pics','sell_price','spec','stock','unit','introduce')
                ->first();

        if($info){
            $info['url'] = url('api/wares/content?id='. $id);
            return self::json($info,'成功');
        }else{
            return $this->error('商品不存在');
        }
    }

    /**
     * 商品介绍(GET) api/wares/content
     */
    public function content(Request $request)
    {
        $id = $request->input('id');
        $content = Wares::where('id', $id)->value('introduce');
       
        header('Content-type: text/html; charset=utf-8');

        $html  = '<html lang="zh-CN">
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1">
                  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
                  <style>img{width: 100% !important; height: auto !important }</style>';

        $html .= $content;
        $html .= '</html>';
        echo $html;
    }

    /**
     * 数据处理
     */
    private function dealData($data)
    {
        return $data;
    }

}